import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';
import 'dart:io';
import '../../resources/kq_theme_colors.dart';

class KqTabView extends StatefulWidget {
  const KqTabView(
      {super.key,
      this.titles,
      this.nomalTxtColors,
      this.nomalBgColors,
      this.nomalBorderColors,
      this.fontSize = 14,
      this.disable = false,
      this.tabIndexBlock});
  final List<String>? titles;
  final List<Color>? nomalTxtColors;
  final List<Color>? nomalBgColors;
  final List<Color>? nomalBorderColors;
//是否允许点击
  final bool disable;
  final double? fontSize;
  final Function(int index)? tabIndexBlock;
  @override
  State<KqTabView> createState() => _KqTabViewState();
}

class _KqTabViewState extends State<KqTabView> {
  //底部横线平分的
  Widget renderAverage(String title, int index) {
    Color txtColor =
        index == 0 ? KqThemeColors.text59 : KqThemeColors.textWhite;
    Color bgColor = index == 0 ? KqThemeColors.bgWhite : KqThemeColors.bgBlue;
    Color borderColor = index == 0 ? KqThemeColors.bgD : KqThemeColors.bgBlue;
    if (widget.nomalTxtColors != null &&
        widget.nomalTxtColors!.length > index) {
      txtColor = widget.nomalTxtColors![index];
    }
    if (widget.nomalBgColors != null && widget.nomalBgColors!.length > index) {
      bgColor = widget.nomalBgColors![index];
    }
    if (widget.nomalBorderColors != null &&
        widget.nomalBorderColors!.length > index) {
      borderColor = widget.nomalBorderColors![index];
    }
    return Opacity(
      opacity:widget.disable ? 0.45 : 1,
      child: KqInkWell(
        onTap: widget.disable
            ? null
            : () {
                if (widget.tabIndexBlock != null) {
                  widget.tabIndexBlock!(index);
                }
              },
        radius: 2,
        borderWidth: 1.0,
        borderColor: borderColor,
        backgroundColor: bgColor,
        child: Container(
          height: 48,
          alignment: Alignment.center,
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: widget.fontSize, color: txtColor),
          ),
        ),
      ),
    );
  }

  List<Widget> renderList() {
    List<Widget> views = [];
    for (var i = 0; i < widget.titles!.length; i++) {
      views.add(Expanded(child: renderAverage(widget.titles![i], i)));
      if (i + 1 != widget.titles!.length) {
        views.add(Container(
          width: 12,
          color: Colors.red,
        ));
      }
    }
    return views;
  }

  @override
  Widget build(BuildContext context) {
    double bottom = 0;
    return Container(
      padding:
          EdgeInsets.only(left: 16, right: 16, top: 8, bottom: bottom),
      color: KqThemeColors.bgWhite,
      child: Row(
        children: renderList(),
      ),
    );
  }
}
